using System;
using System.Collections;
using MirLda.BL.Interfaces;

namespace MirLda.BL.Implements
{
	/// <summary>
	/// Configuration for loading data from configuration
	/// </summary>
	internal class Configuration:IConfiguration
	{
		#region fields

		private static Configuration _configurationInstance;
		private static Hashtable _configCache;

		#endregion

		#region constructor
		private Configuration()
		{}
		#endregion

		#region constants

		private const string KEY_CONN_STRING				= "ConnectionString";
		private const string KEY_SITE_URL					= "Site.URL";
		private const string KEY_SITE_PATH					= "Site.Path";
		private const string KEY_SITE_NEWS_RECORDSONPAGE	= "Site.News.RecordsOnPage";

		#endregion

		#region properties

		public int News_RecordsOnPage
		{
			get{ return GetConfigInt(KEY_SITE_NEWS_RECORDSONPAGE); }
		}

		public string ConnectionString
		{
			get { return GetConfigString(KEY_CONN_STRING); }
		}

		public string SiteUrl
		{
			get { return GetConfigString(KEY_SITE_URL); }
		}

		public string SitePath
		{
			get { return GetConfigString(KEY_SITE_PATH); }
		}

		#endregion

		#region methods

		internal static Configuration GetInstance()
		{
			if( _configurationInstance == null )
			{
				_configurationInstance = new Configuration();
			}
			return _configurationInstance;
		}

		private int GetConfigInt( string key )
		{
			string str = GetConfigString(key);
			if( str.Length == 0 )
				return -1;
			else
				return Int32.Parse( str );
		}

		private string GetConfigString( string key )
		{
			if( _configCache == null )
			{
				_configCache = new Hashtable();
			}

			if( _configCache[key] == null )
			{
				string configValue = System.Configuration.ConfigurationSettings.AppSettings[key];
				_configCache[key] = configValue;
			}
			return Utils.StrOrEmpty( _configCache[key].ToString() );
		}

		#endregion
	}
}
